package io.github.jesse205.dragdrop.pro.extensions

import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.WindowCompat
import io.github.jesse205.dragdrop.pro.R

/**
 * 设置 Activity 的工具栏
 * @param isBackButtonEnabled 是否启用返回键
 * */
fun AppCompatActivity.setupToolbar(isBackButtonEnabled: Boolean = false) {
    val toolbar: Toolbar? = findViewById(R.id.toolbar)
    toolbar?.let { setupToolbar(it, isBackButtonEnabled) }
}

/**
 * 设置 Activity 的工具栏
 * @param toolbar 指定的工具栏
 * @param isBackButtonEnabled 是否启用返回键
 * */
fun AppCompatActivity.setupToolbar(toolbar: Toolbar, isBackButtonEnabled: Boolean = false) {
    setSupportActionBar(toolbar)
    supportActionBar?.setDisplayHomeAsUpEnabled(isBackButtonEnabled)
}

fun AppCompatActivity.setSubtitle(subtitle: CharSequence) {
    assert(supportActionBar != null) { "supportActionBar is null" }
    supportActionBar!!.subtitle = subtitle
}

fun AppCompatActivity.fixSystemBars(
    statusBarContrast: Boolean = true, navigationBarContrast: Boolean = true
) {
    val insetsController = WindowCompat.getInsetsController(window, window.decorView)

    WindowCompat.setDecorFitsSystemWindows(window, false)
    window.statusBarColor =
        if (!statusBarContrast) Color.TRANSPARENT else getThemeColor(android.R.attr.statusBarColor)
    window.navigationBarColor =
        if (!navigationBarContrast && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            Color.TRANSPARENT else getThemeColor(android.R.attr.navigationBarColor)

    if (getThemeBoolean(R.attr.windowLightStatusBar)) {
        insetsController.isAppearanceLightStatusBars = true
    }
    if (getThemeBoolean(R.attr.windowLightNavigationBar)) {
        insetsController.isAppearanceLightNavigationBars = true
    }
}